package day0727API;

public class Test1 {
    public static void main(String[] args) {
        Bank  bank=new Bank();
        Thread t1=new Thread(){
            public void run() {
                while(true){
                    bank.getMoney();//取款
                    int rs=bank.checkUserMoney();//查询余额
                    System.out.println(rs);
                    if(rs==0){//如果查询结果是0(余额为0)就结束循环，不能再取款了
                        break;
                    }
                    Thread.yield();//让t1进入Runnable状态，让t2运行
                }
            }
        };
        t1.start();

        Thread t2=new Thread(()-> {
            while(true){
                bank.getMoney();//取款
                int rs=bank.checkUserMoney();//查询余额
                System.out.println(rs);
                if(rs==0){//如果查询结果是0(余额为0)就结束循环，不能再取款了
                    break;
                }
                Thread.yield();//让t1进入Runnable状态，让t2运行
            }
        });
        t2.start();
    }

}


class Bank{
    int userMoney=10;
    public int getMoney(){//取钱
        userMoney--;
        return 1;
    }
    public int checkUserMoney(){//查询余额
        return userMoney;
    }
}
